/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package invsys.Controladores;


import invsys.Modelo.Cliente;
import invsys.Utilidades.ManejadorBaseDatos;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author luigie
 */
public class ControlCliente {
  public static Cliente load(ResultSet rs) throws SQLException{
        Cliente c=new Cliente();
        c.setId(rs.getString(1));
        c.setNombre(rs.getString(2));
        c.setApellido(rs.getString(3));
        c.setDireccion(rs.getString(4));
        c.setTelefono(rs.getString(5));
        c.setCelular(rs.getString(6));
        c.setDescuento(rs.getDouble(7));
        c.setSaldo(rs.getDouble(8));
             
        return c;
    }   
  private static List clientes;
  public static Connection con;
  public static void RegistrarCliente(Cliente c) throws SQLException, IOException, Exception {
        ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
        mbd.conectar();
        con = mbd.getConexion();
        if (con == null) {
            throw new SQLException(" no hay conexion ");
        }
        PreparedStatement pst = null;

        try {

            pst = con.prepareStatement("Insert Into clientes values(?,?,?,?,?,?,?,?)");
            pst.setString(1, c.getId());
            pst.setString(2, c.getNombre());
            pst.setString(3, c.getApellido());
            pst.setString(4, c.getDireccion());
            pst.setString(5, c.getTelefono());
            pst.setString(6, c.getCelular());
            pst.setDouble(7, c.getDescuento()); 
            pst.setDouble(8, c.getSaldo());
            pst.executeUpdate();
           
        } finally {
            if (pst != null) {
                pst.close();
            }
        }
    }
  public static Cliente BuscarClienteid(String ide) throws SQLException{
            if (ide == null) {
          throw new SQLException("No hay elemento clave de la clase usuario");
        }
         ResultSet rs = null;
         PreparedStatement pst = null;
         Cliente cliente= null;
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
         pst = con.prepareStatement("select * from clientes where id= ?");
         pst.setString(1, ide.trim());

         rs = pst.executeQuery();
         while(rs.next()) {
             cliente = load(rs);
         }
    } finally {
         if (rs != null) {
            rs.close();
         }
         if (pst != null) {
           pst.close();
         }
          return cliente;
    }
    }
  public static Cliente BuscarClientenombre(String nom) throws SQLException{
            if (nom == null) {
          throw new SQLException("No hay elemento clave de la clase Clientes");
        }
         ResultSet rs = null;
         PreparedStatement pst = null;
         Cliente cliente = null;
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
         pst = con.prepareStatement("select * from clientes where nombre= ?");
         pst.setString(1, nom.trim());

         rs = pst.executeQuery();
         while(rs.next()) {
             cliente = load(rs);
         }
    } finally {
         if (rs != null) {
            rs.close();
         }
         if (pst != null) {
           pst.close();
         }
          return cliente;
    }
    }
  public static void ModificarCliente(Cliente c, String ide) throws SQLException, Exception {
        if (BuscarClienteid(ide) == null) {
            throw new SQLException("usuario no registrado ");
        }
        PreparedStatement pst = null;

        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("UPDATE clientes SET id = ? ,"
                    + " nombre = ?,apellido = ?, direccion = ?, telefono = ? ,"
                    + " celular = ?, saldo= ?, descuento=? "
                    + " WHERE id = ? ");	
            pst.setString(1, c.getId());
            pst.setString(2, c.getNombre());
            pst.setString(3, c.getApellido());
            pst.setString(4, c.getDireccion());
            pst.setString(5, c.getTelefono());
            pst.setString(6, c.getCelular());
            pst.setString(9, ide);
            pst.setDouble(7, c.getSaldo());
            pst.setDouble(8, c.getDescuento());
            System.out.println(pst.toString());
            pst.executeUpdate();

        } finally {
            if (pst != null) {
                pst.close();
            }
        }

    }
  public static void listarCliente() throws SQLException, Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     clientes = new LinkedList();
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement("select * from clientes ");
        rs = pst.executeQuery();
        while(rs.next()) {
           clientes.add(load(rs));
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
}
}
  public static boolean ExisteCliente(int ide){
        boolean existe=false;

        return existe;
    }
  public static void EliminarCliente(String ide) throws SQLException, Exception {
        if (BuscarClienteid(ide) == null) {
            throw new SQLException("No hay elemento clave de la clase Clientes");
        }
        PreparedStatement pst = null;

        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("delete from clientes where id = ?");
            pst.setString(1, ide.trim());
            pst.executeUpdate();

        } finally {
            if (pst != null) {
                pst.close();
            }
        }

    }
  public static void listarCliente1(String sql) throws SQLException, Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     clientes = new LinkedList();
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();
        while(rs.next()) {
           clientes.add(load(rs));
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
}
}
   public static double SaldoCredito(String cliente) throws SQLException, Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     double ultimoc=0;
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement("SELECT SUM(saldo) AS saldo FROM creditos WHERE cliente='"+cliente+"'");
        rs = pst.executeQuery();
        while(rs.next()) {
           ultimoc=rs.getDouble("saldo");
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
    }
    return ultimoc;
}
  
  public static void setCliente(List aEmpleado) {
        clientes = aEmpleado;
    }
  public static List getCliente() {
        return clientes;
    }


}
